package com.xintonghua.bussiness.ui;


import android.Manifest;
import android.os.Bundle;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.util.DownloadUtil;

import butterknife.ButterKnife;

public class DownApkActivity extends BaseActivity {
    String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_down_apk);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        url = getIntent().getStringExtra("url");
        requestPermissions();
    }


    private void requestPermissions() {
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission.requestEach(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(permission -> {
                    if (permission.granted) {
                        DownloadUtil.get().installApk(this, url);
                    } else if (permission.shouldShowRequestPermissionRationale) {

                    } else {
                        finish();
                    }
                });

    }

}
